package com.sd.sms.cmpp;

import com.sd.common.beans.HttpBalanceInfo;
import com.sd.common.beans.HttpSendInfo;
import com.sd.common.beans.HttpSmsInfo;
import com.sd.common.utils.JerseyUtil;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 阳光金点短信接口
 */
@Component
public class HttpSmsUtils {

    private static final Logger logger = LoggerFactory.getLogger(HttpSmsUtils.class);
    //请求发送短信地址
    private static String url = "http://47.92.236.162:8122/smsapi/sms/sendmt";

    private static String balanceUrl="http://47.92.236.162:8122/smsapi/sms/getbalance";
    //机构代码
    private static String orgcode = "XIKF";
    //业务类型
    private static String btype = "B-XIKF";
    //密码
    private static String password = "xikf123456!";

    private static String sign="【小i客服】";

    public static String httpSendSms(List<HttpSmsInfo> httpSmsInfos){

        List<HttpSmsInfo> newHttpSmsList = new ArrayList<>();

        if(null == httpSmsInfos || httpSmsInfos.size()<1){
            return "需要发送的短信不能为空";
        }

        for(HttpSmsInfo httpSmsInfo : httpSmsInfos){
            if(StringUtils.isBlank(httpSmsInfo.getContent()) || StringUtils.isBlank(httpSmsInfo.getMobile())){
                return "短信内容或手机号码不能为空";
            }

            String uuid = UUID.randomUUID().toString().replaceAll("-","");

            //将需要发送的每一条短信产生唯一的id
            HttpSmsInfo newHttpSmsInfo = new HttpSmsInfo();
            newHttpSmsInfo.setContent(sign+httpSmsInfo.getContent());
            newHttpSmsInfo.setMobile(httpSmsInfo.getMobile());
            newHttpSmsInfo.setVid(uuid);

            newHttpSmsList.add(newHttpSmsInfo);

        }

        String timestamp = getTimestamp();

        //执行需要发送的短信内容
        logger.info("-------需要发送的短信内容-----newHttpSmsList:[{}]----",newHttpSmsList.toString());
        HttpSendInfo httpSendInfo = new HttpSendInfo();
        httpSendInfo.setOrgcode(orgcode);
        httpSendInfo.setBtype(btype);
        httpSendInfo.setEstr(getAuthenticatorSource(orgcode,btype,timestamp,password));
        httpSendInfo.setTimestamp(timestamp);
        httpSendInfo.setHttpSmsInfo(newHttpSmsList);

        //将数据转化成json格式
        JSONObject json = JSONObject.fromObject(httpSendInfo);

        logger.info("--------{HttpSmsUtils->execute}----request Remote url:[{}]---------request:[{}]",url,json.toString());
        long beginTime = System.currentTimeMillis();
        //执行发送短信请求
        String result = JerseyUtil.responseJson(url,json.toString());
        logger.info("--------{HttpSmsUtils->execute}----调用发送短信地址Remote url:[{}]-----------response:[{}]",url,result);
        long endTime = System.currentTimeMillis();
        logger.info("--------{HttpSmsUtils->execute}----调用发送短信地址:[{}]---system time consuming:[{}]ms-----------",url,(endTime-beginTime));

        return result;
    }

    public static String getbalance(){

        String timestamp = getTimestamp();

        HttpBalanceInfo httpBalanceInfo = new HttpBalanceInfo();
        httpBalanceInfo.setBtype(btype);
        httpBalanceInfo.setOrgcode(orgcode);
        httpBalanceInfo.setTimestamp(timestamp);
        httpBalanceInfo.setEstr(getAuthenticatorSource(orgcode,btype,timestamp,password));

        //将数据转化成json格式
        JSONObject json = JSONObject.fromObject(httpBalanceInfo);

        //执行获取余额请求
        String result = JerseyUtil.responseJson(balanceUrl,json.toString());

        return result;
    }

    //获取密钥，orgcode+btype+timestamp+password进行 MD5 加密,相关参数由阳光金点提供
    public static String getAuthenticatorSource(String orgcode, String btype, String timestamp, String password){

        String mdsecret = DigestUtils.md5Hex(orgcode+btype+timestamp+password);

        return mdsecret;
    }

    //获取时间戳，格式为YYYYMMDDHHMISS
    public static String  getTimestamp(){

        DateFormat format=new SimpleDateFormat("yyyyMMddhhmmss");

        return format.format(new Date());
    }

}
